#include <TStyle.h>
#include <string>
#include <TH1.h>
#include <TH1F.h>
#include <TH2F.h>
#include <TAxis.h>
#include <TCanvas.h>
#include <TPad.h>
#include <TFile.h>
#include <TLegend.h>

using namespace std;

void q2D(std::string var, std::string tituloX, std::string tituloY)
{
  Int_t icol=0;
  gStyle->SetFrameBorderMode(icol);
  gStyle->SetFrameFillColor(icol);
  gStyle->SetCanvasBorderMode(icol);
  gStyle->SetCanvasColor(icol);
  gStyle->SetPadBorderMode(icol);
  gStyle->SetPadColor(icol);
  gStyle->SetStatColor(icol);

//  gStyle->SetPaperSize(26,20);
  gStyle->SetPadTopMargin(0.05);
  gStyle->SetPadRightMargin(0.26);
  gStyle->SetPadBottomMargin(0.10);
  gStyle->SetPadLeftMargin(0.10);

  gStyle->SetTitleXOffset(1.3);
  gStyle->SetTitleYOffset(1.3);
  Int_t font=42;
  Double_t tsize=0.05;
  gStyle->SetTextFont(font);
  gStyle->SetTextSize(tsize);
  gStyle->SetLabelFont(font,"x");
  gStyle->SetTitleFont(font,"x");
  gStyle->SetLabelFont(font,"y");
  gStyle->SetTitleFont(font,"y");
  gStyle->SetLabelFont(font,"z");
  gStyle->SetTitleFont(font,"z");
  gStyle->SetLabelSize(tsize,"x");
  gStyle->SetTitleSize(tsize,"x");
  gStyle->SetLabelSize(tsize,"y");
  gStyle->SetTitleSize(tsize,"y");
  gStyle->SetLabelSize(tsize,"z");
  gStyle->SetTitleSize(tsize,"z");
  gStyle->SetMarkerStyle(20);
  gStyle->SetMarkerSize(0.7);
  gStyle->SetHistLineWidth(2);
  gStyle->SetLineStyleString(2,"[12 12]");
  //gStyle->SetEndErrorSize(0.);
  gStyle->SetOptTitle(0);
  gStyle->SetOptStat(1111111);
  //gStyle->SetOptStat(0);
  gStyle->SetOptFit(0);
  gStyle->SetPadTickX(1);
  gStyle->SetPadTickY(1);
  gStyle->SetPalette(1);
  int nfiles=9;
  TFile *f[nfiles];
  f[0] = TFile::Open("../sumbg/plots.root","READ");
  f[1] = TFile::Open("../qcd/plots.root","READ");
  f[2] = TFile::Open("../ttbar/plots.root","READ");
  f[3] = TFile::Open("../dibosons/plots.root","READ");
  f[4] = TFile::Open("../zplusjets/plots.root","READ");
  f[5] = TFile::Open("../wplusjets/plots.root","READ");
  f[6] = TFile::Open("../singletop/plots.root","READ");
  f[7] = TFile::Open("../signal/plots.root","READ");
  f[8] = TFile::Open("../real/plots.root","READ");
  //f[8] = TFile::Open("../../doplotsalaemma/plots.root","READ");


  TH2F* hsumbg;
  TH2F* hvar1;
  TH2F* hvar2;
  TH2F* hvar3;
  TH2F* hvar4;
  TH2F* hvar5;
  TH2F* hvar6;
  TH2F* hsignal;
  TH2F* hreal;


  //hreal->Sumw2();
  //hsumbg->Sumw2();
  hsumbg = (TH2F*)f[0]->Get(var.c_str());
  hvar1 = (TH2F*)f[1]->Get(var.c_str());
  hvar2 = (TH2F*)f[2]->Get(var.c_str());
  hvar3 = (TH2F*)f[3]->Get(var.c_str());
  hvar4 = (TH2F*)f[4]->Get(var.c_str());
  hvar5 = (TH2F*)f[5]->Get(var.c_str());
  hvar6 = (TH2F*)f[6]->Get(var.c_str());
  hsignal = (TH2F*)f[7]->Get(var.c_str());
  hreal = (TH2F*)f[8]->Get(var.c_str());

 
  std::string tc = "c"+var;
  TCanvas *cvar = new TCanvas(tc.c_str(),tc.c_str());
  cvar->cd();
  //cvar->SetGrid();
  //p1var->SetLogy();
  TAxis *yaxisvar = hsignal->GetYaxis();
  //yaxisvar->SetRangeUser(ry1,ry2);
  yaxisvar->SetTitle(tituloY.c_str());
  hsumbg->SetLineColor(kRed);
  hsignal->GetXaxis()->SetTitle(tituloX.c_str());
  hsumbg->SetStats(0);
  hsignal->SetFillColor(kAzure+1);
  hsignal->SetLineColor(kAzure+1);
  hreal->SetMarkerColor(kBlack);
  hreal->SetMarkerStyle(3);
  hreal->SetMarkerSize(0.7);

  hsignal->Draw("BOX");
  hsumbg->Draw("SAMEBOX");
  hreal->Draw("PSAMES");
  TLegend* ley1var = new TLegend(.75,.18,.98,.50);
  ley1var->AddEntry(hreal,"data L=35pb^{-1}","P");
  ley1var->AddEntry(hsumbg,"backgrounds","F");
  ley1var->AddEntry(hsignal,"BRPV SU3","F");
  ley1var->SetFillColor(kWhite);
  ley1var->Draw("SAME");
  cvar->cd();
  cvar->SetSelected(cvar);
  std::string argpng = "ploticos/"+var+".png";
  cvar->SaveAs(argpng.c_str());
  cvar->Close();
  //cvar->Write();

  //return ccvar;


}

